package User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class UserInfo {
    private String username;
    private String password;
    private String surname;
    private String name;
    private String mail;

    public UserInfo(){

    }
    public UserInfo(String username, String password, String surname, String name, String mail) {
        this.username = username;
        this.password = password;
        this.surname = surname;
        this.name = name;
        this.mail = mail;
    }
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public boolean CheckLogin(String username,String password) throws Exception{
        FileReader fileReader=null;
        BufferedReader buffer=null;
        String line[]=null;
        try {
            fileReader = new FileReader("注册信息.txt");
            buffer = new BufferedReader(fileReader);
            String lines = null;
            while ((lines = buffer.readLine()) != null) {
                line = lines.split(" ");
                if (username.equals(line[0]) && password.equals(line[1])) {
                    return true;
                }
            }
            fileReader.close();
        }catch (IOException e){
            e.printStackTrace();
        }
        return false;
    }
    public boolean CheckReigster(String username){
        try {
            FileReader fr = new FileReader("注册信息.txt");
            BufferedReader bf = null;
            bf = new BufferedReader(fr);
            String lines = null;
            String line[] = null;
            while ((lines = bf.readLine()) != null) {
                line = lines.split(" ");
                if (username.equals(line[0])) {
                    return false;
                }
            }
        }catch (Exception e){}
        return true;
    }
    public void SaveDgut(Userjson user)
    {
        try {
            FileReader fr = null;
            BufferedReader bf = null;
            String lines = null;
            String line[] = null;
            int flag = 0;
            fr = new FileReader("注册信息.txt");
            bf = new BufferedReader(fr);
            while ((lines = bf.readLine()) != null) {
                line = lines.split(" ");
                if (user.getUsername().equals(line[0])) {
                    flag = 1;
                    break;
                }
            }
            fr.close();
            if (flag == 0) {
                String surname = user.getName().substring(0, 1);
                String newname = user.getName().substring(1, user.getName().length());
                FileWriter fw = new FileWriter("注册信息.txt", true);
                fw.write(user.getUsername() + " " + "123456" + " " + surname + " " + newname +" "+ user.getUsername()+"@qq.com"+"\r\n");
                fw.flush();
                fw.close();
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    public void SaveUser(UserInfo user){
        try {
            FileWriter fw = new FileWriter("注册信息.txt", true);
            fw.write(user.getUsername()+ " " + user.getPassword() + " " + user.getSurname() + " " + user.getName() + " " + user.getMail() + "\r\n");
            fw.flush();
            fw.close();
        }catch (Exception e){}
    }
    public UserInfo Login(String u,String p){
        UserInfo us=new UserInfo();
        try {
            FileReader r=new FileReader("注册信息.txt");
            BufferedReader br=new BufferedReader(r);
            String lines=null;
            String line[]=null;
            while ((lines = br.readLine()) != null) {
                line = lines.split(" ");
                if (u.equals(line[0])&&p.equals(line[1])) {
                    us=new UserInfo(line[0],line[1],line[2],line[3],line[4]);
                    return us;
                }
            }
        }catch (Exception e){}
        return us;
    }
}
